package cn.sykj.car.controller;


import cn.sykj.car.common.ResponseBean;
import cn.sykj.car.common.StatusCode;
import cn.sykj.car.dict.DeleteFlagEnum;
import cn.sykj.car.entity.CarStore;
import cn.sykj.car.entity.Users;
import cn.sykj.car.service.ICarStoreService;
import cn.sykj.car.service.IUsersService;
import cn.sykj.car.utils.Utils;
import cn.sykj.car.vo.CarStoreVO;
import cn.sykj.car.vo.ResponseMessageVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 门店信息 前端控制器
 * </p>
 *
 * @author xuming
 * @since 2021-06-17
 */
@RestController
@RequestMapping("/carStore")
@Slf4j
public class CarStoreController extends BaseController {

    @Resource
    private ICarStoreService iCarStoreService;

    @Resource
    private IUsersService iUsersService;

    /**
     * 添加店铺
     *
     * @param carStore
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "添加店铺", notes = "添加店铺")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("carStore:create")
    @RequestMapping(value = "/addCarStore", method = RequestMethod.POST)
    public ResponseBean addCarStore(@RequestBody CarStore carStore) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        Users user = iUsersService.getUserByName((String) obj);

        carStore.setCreateTime(new Date());
        carStore.setCreateUser(user.getUserName());
        carStore.setUpdateTime(new Date());
        carStore.setUpdateUser(user.getUserName());
        carStore.setDelFlag(DeleteFlagEnum.FALSE.VALUE);

        ResponseMessageVO responseMessageVO = iCarStoreService.addCarStore(carStore);
        if (Utils.notEmpty(responseMessageVO) && responseMessageVO.isRetFlag()) {
            this.writeLog2Manager(responseMessageVO);
        }
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }

    /**
     * 删除门店信息
     *
     * @param id
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "删除门店信息", notes = "删除门店信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("carStore:delete")
    @RequestMapping(value = "/{id}/delete", method = RequestMethod.POST)
    public ResponseBean delete(@PathVariable Integer id) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        ResponseMessageVO responseMessageVO = iCarStoreService.delete(id);
        if (Utils.notEmpty(responseMessageVO) && responseMessageVO.isRetFlag()) {
            this.writeLog2Manager(responseMessageVO);
        }
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }

    /**
     * 门店信息查询
     *
     * @param carStoreVO
     * @param currentPage
     * @param pageSize
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "门店信息查询", notes = "门店信息查询")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("carStore:view")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public ResponseBean pagCarStore(@RequestBody CarStoreVO carStoreVO,
                                    @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer currentPage,
                                    @RequestParam(value = "pageSize", required = false, defaultValue = "10") Integer pageSize
    ) throws Exception {


        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        IPage<CarStoreVO> page = new Page(currentPage, pageSize);
        page = iCarStoreService.pageCarStore(page, carStoreVO);
        return new ResponseBean(StatusCode.OK, page);
    }

    /**
     * 修改门店信息
     *
     * @param carStore
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "修改门店信息", notes = "修改门店信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("carStore:edit")
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseBean updateCarStore(@RequestBody CarStoreVO carStore) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        // 当前登录人的获取
        Users user = iUsersService.getUserByName((String) obj);
        carStore.setUpdateTime(new Date());
        carStore.setUpdateUser(user.getUserName());

        ResponseMessageVO responseMessageVO = iCarStoreService.updateCarStore(carStore);
        this.writeLog2Manager(responseMessageVO);
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }

    /**
     * 审核门店信息
     *
     * @param carStoreVO
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "审核门店信息", notes = "审核门店信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("carStore:check")
    @RequestMapping(value = "/checkCarStore", method = RequestMethod.POST)
    public ResponseBean checkCarStore(@RequestBody CarStoreVO carStoreVO) throws Exception {

        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        Users users = iUsersService.getUserByName((String) obj);
        carStoreVO.setCheckUser(users.getUserName());
        carStoreVO.setCheckTime(new Date());

        ResponseMessageVO responseMessageVO = iCarStoreService.checkCarStore(carStoreVO);
        if (Utils.notEmpty(responseMessageVO) && responseMessageVO.isRetFlag()) {
            this.writeLog2Manager(responseMessageVO);
        }
        return new ResponseBean(StatusCode.OK, responseMessageVO);
    }

    /**
     * 导出门店信息
     *
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(hidden = false, value = "导出门店信息", notes = "导出门店信息")
    @ApiImplicitParams({})
    @RequiresAuthentication
    @RequiresPermissions("carStore:export")
    @RequestMapping(value = "/exportCarStore", method = RequestMethod.POST)
    public void exportCarStore(@RequestBody CarStoreVO carStoreVO, HttpServletResponse response
    ) throws Exception {
        // 获取登陆系统用户名，存入日志
        Object obj = SecurityUtils.getSubject().getPrincipal();
        log.debug((String) obj);

        List<CarStoreVO> list = iCarStoreService.exportCarStore(carStoreVO);


        this.exportExcel(response, list, "导出门店信息");
    }
}
